﻿using System.Buffers.Text;
using System.Buffers;
using System.Text.Json;
using System;
using System.Text.Json.Serialization;
using Masuit.Tools;

namespace adb
{
    /// <summary>
    /// 
    /// </summary>
    public class DecimalConverter : JsonConverter<decimal>
    {
        public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
                return reader.GetString().ConvertTo<decimal>();
            if (reader.TokenType == JsonTokenType.Number)
                return reader.GetDecimal();
            throw new Exception("JSON数据格式出错");
        }



        public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString("#0.#########"));
            //writer.WriteNumberValue(value);
        }
    }
}
